/**
*      author: Y_Yao
*      created: 2024-11-02 22:57:27
**/

#include<bits/stdc++.h>

using namespace std;
#define int LL
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 7;
int B[N],C[N];
int n,k;
int a[N];

void solve()
{
    cin >> n >> k;
    memset(a,0,sizeof a);
    for(int i = 1;i <= k;i++)
    {
        int b,c;
        cin >> b >> c;
        a[b] += c;
    }

    priority_queue<int> heap;
    for(int i = 1;i <= k;i++)
    {
        heap.push(a[i]);
    }

    int ret = 0;
    for(int i = 1;i <= n;i++)
    {
        int tmp = 0;
        if(!heap.empty())
        {
            tmp = heap.top();
            heap.pop();
        }
        ret += tmp;
    }

    cout << ret << endl;

}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}